<template>
  <section>
    <!-- 面包屑 -->
      <breadcrumbs></breadcrumbs>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="商品名称"
                    prop="">
        <el-input v-model="searchForm.info"
                  placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"
          class="btn-box"></btns>
    <div class="data-container scroll-table">
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                class="table-box"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection" > </el-table-column>
        <el-table-column type="index"
                         label="序号"
                         width="60">
        </el-table-column>
        <el-table-column prop="bandImg"
                         label="产品图">
          <template slot-scope="scope">
            <img :src="scope.row.bandImg"
                 class="img-box"
                 alt="">
          </template>
        </el-table-column>
        <el-table-column prop="brandName"
                         label="产品名称">
        </el-table-column>
        <el-table-column prop="backMoney"
                         label="回收价格(元)">
               <template slot-scope="scope">
                <p v-if="scope.row.backMoney==null||scope.row.backMoney==undefined">0</p>
                <p v-else>{{scope.row.backMoney/100}}</p>
               </template>
        </el-table-column>
         <el-table-column prop="state"
                         label="状态">
            <template slot-scope="scope">
                 <p v-if="scope.row.state==1">启用</p>
                 <p v-if="scope.row.state==2">禁用</p>
            </template>
        </el-table-column>
        <el-table-column prop="addtime"
                         label="创建时间">
        </el-table-column>
        <!-- <el-table-column label="操作"
                         width="135px">
          <template slot-scope="scope">
            <el-button @click="editbrand(scope.row)">修改</el-button>
          </template>

        </el-table-column> -->
      </el-table>
    </div>
    <!--新增修改界面-->
    <el-dialog title="新增回收品牌"
               :visible.sync="AddFormVisible">
      <el-form :model="addForm"
               ref="addForm"
               :rules="rules">
        <el-form-item label="产品图"
                      :label-width="formLabelWidth"
                      prop="">


          <el-upload
            :action="importFileUrl"
            v-model="addForm.imageUrl"
            list-type="picture-card"
            :before-upload="beforeAvatarUpload"
            :auto-upload="true"
            :on-success="getUrl"
            :accept="'image/*'"
            :show-file-list="false"
          >
            <img
              v-if="imageUrl"
              :src="imageUrl"
              class="img-feng"
              alt="图片"
            />
            <i v-else class="el-icon-plus"></i>
          </el-upload>
          
          <!-- <el-upload class="avatar-uploader"
                     :action="importFileUrl"
                     :show-file-list="false"
                     :limit="1"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl"
                 :src="imageUrl"
                 class="avatar">
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload> -->
          <!-- <el-upload :action="importFileUrl"
                     :show-file-list="false"
                     list-type="picture-card"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload"
                     :auto-upload="false">
            <i slot="default"
               class="el-upload-list__item-thumbnail"></i>
            <div slot="file"
                 slot-scope="{file}">
              <img class="el-upload-list__item-thumbnail"
                   :src="file.url"
                   alt="">
              <span v-if="!disabled"
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)">
                <i class="el-icon-delete"></i>
              </span>
            </div>

          </el-upload> -->

        </el-form-item>
        <el-form-item label="品牌名称"
                      :label-width="formLabelWidth"
                      prop="name">
          <el-input v-model="addForm.name"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="回收价格(元)"
                      :label-width="formLabelWidth"
                      prop="backMoney">
          <el-input v-model="addForm.backMoney"
                    autocomplete="off" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="AddFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submit('addForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- <el-dialog :visible.sync="dialogVisible">
      <img width="100%"
           :src="dialogImageUrl"
           alt="">
    </el-dialog> -->
  </section>
</template>

<script>
import btns from '@/components/btns.vue';
import breadcrumbs from "@/components/breadcrumbs.vue";
import {unitPriceValidate} from "../../utils/tool";
import {
  format
} from "../../utils/tool"
export default {
  components: {
    btns,
    breadcrumbs,
  },
  data () {
    return {
      xixi:'',
      importFileUrl: this.GLOBAL.imgBaseUrl,
      imageUrl: "",
      userData: [],
      searchName: "",
      houseArr: [],
      adduserIds: [],
      formLabelWidth: "120px",
      AddFormVisible: false,
      addForm: {
        houseIds: []
      },
      currType: 1,
      yearData: "",
      topData: {},
      btnForm: {},
      userForm: {
        current: 1,
        size: 10,
        totalCount: 0,

      },
      searchForm: {
        name: ''
      },
      addORUpdateVisible: false,
      funTit: '',
      cityForm: {
        cityName: "",
        cityId: ''
      },
      rules: {
        "btnIcon": [],
        "info": [{
          required: true,
          message: '请输入品牌名称',
          trigger: 'blur'
        }],
        "price": [{
          required: true,
          message: '请输入回收价格',
          trigger: 'blur'
        }],
        "sort": [{
          required: true,
          message: '排序',
          trigger: 'blur'
        }],
        "cid": [{
          required: true,
          message: '请选择所在地址',
          trigger: 'change'
        }],
        "name": [{
          required: true,
          message: '请输入品牌名称',
          trigger: 'blur'
        }],
        "address": [{
          required: true,
          message: '请输入详细地址',
          trigger: 'blur'
        }],
        "shopName": [{
          required: true,
          message: '请输入采购公司名称',
          trigger: 'blur'
        }],
        "title": [{
          required: true,
          message: '请输入消息标题',
          trigger: 'blur'
        }],
        "userType": [{
          required: true,
          message: '请选择用户接收终端类型',
          trigger: 'change'
        }],
        "info": [{
          required: true,
          message: '请输入要发送的消息主体',
          trigger: 'blur'
        }],
        "backMoney": [{
          required: true,
          message: '请输入回收价格',
          trigger: 'blur'
        },
        { validator: unitPriceValidate, trigger: 'blur' }
        ],
      },
      tableData: [],
      tableSelection: [],
      hasFile:false
    }
  },
  created () {
    this.getTableData(this.searchForm.info);
  },
  methods: {
     // 截取当前数据到小数点后两位
    numFilter(value) {
      const realVal = parseFloat(value).toFixed(2);
      return realVal;
    },
     getUrl(item) {
      this.hasFile = true;
      // console.log(this.addfile);
      // this.addfile = item.data.url;
      this.addForm.imageUrl = item;
      this.imageUrl = item;
      // console.log(item);
      // this.isShow = true;
      this.$message({
        type: "success",
        message: "图片上传成功",
      });
      // console.log(this.editimg);
    },
    editbrand (row) {
      this.AddFormVisible = true;
      this.funTit = 'update';
      console.log(row)
      this.addForm = row;
      this.addForm = {
        backMoney: row.backMoney/100,
        name: row.brandName,
        brandId: row.brandId
      }
    
      this.imageUrl = row.bandImg;
    },
    submit (formName) {
      console.log(this.imageUrl);
      this.xiaoshu = Math.ceil(this.numFilter(this.addForm.backMoney));
      console.log("保留两位小数"+this.xiaoshu)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.funTit == 'add') {
            this.$api.brand.addBrand({
              backPrice: parseInt(this.addForm.backMoney*100) ,
              brandImg: this.imageUrl,
              brandName: this.addForm.name
            }).then(d => {
              if (d.code == 200) {
                this.addForm = {};
                this.imageUrl = '';
                this.$message({
                  type: "success",
                  message: "添加成功"
                })
                // this.$refs.btnForm.resetFields();
                this.AddFormVisible = false;
                this.getTableData(this.searchForm.info);
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            // console.log(this.addForm, 12313)
            this.xixi= Math.ceil(this.numFilter(this.addForm.backMoney)*100);
            this.$api.brand.editBrand(
              {
                backPrice: this.xixi,
                brandId: this.addForm.brandId,
                brandImg: this.imageUrl,
                brandName: this.addForm.name
              }

            ).then(d => {
              if (d.code == 200) {
                this.imageUrl = '';
                this.getTableData(this.searchForm.info);
                this.AddFormVisible = false;
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = '';
      console.log(res)
      this.imageUrl = res;
    },
    beforeAvatarUpload (file) {
      console.log(file, 231232)
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return isLt2M;
    },
    gototogetHouseMent (row) {
      console.log(row)
      this.$router.push({
        path: "/message/mesageDetails",
        query: {
          messageId: row.messageId
        }
      })
    },
    getHouseArr () {
      this.$api.operate.getNotCityHouseList({
        cityId: 1,
      }).then(res => {
        if (res.code == 200) {
          this.houseArr = res.data;
        }
      })
    },
    getHouse () {
      console.log(this.addForm.houseIds)
    },
    sendMessage (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api.user.sendMessage({
            content: this.addForm.info,
            recIds: this.adduserIds.length >= 0 ? this.adduserIds.join(",") : '',
            recType: this.currType,
            title: this.addForm.title
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "发送消息成功"
              })
              this.adduserIds = [];
              this.currType = 0;
              this.AddFormVisible = false;
              this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid, this.searchForm.gid);
              this.addForm = {};
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    addhouse () {
      this.AddFormVisible = true
    },
    handleSizeChange (v) {
      console.log(v)
      this.searchForm.size = v;
      this.getTableData(this.searchForm.current, v, this.searchForm.info);
    },
    handleCurrentChange (v) {
      this.searchForm.current = v;
      this.getTableData(v, this.searchForm.size, this.searchForm.info);
    },
    getTableData (info) {
      this.$api.brand.getAdminBrandList({
        brandName: info
      }).then(res => {
        if (res.code == 200) {
          this.tableData = res.data;
          console.log(this.tableData)
          this.searchForm.totalCount = res.totalCount;
          this.searchForm.size = res.pageSize
        }
      })
    },
    chageYearData () {
      this.$api.recycleBin.getAdminBackStopCount({
        year: this.yearData
      }).then(res => {
        if (res.code == 200) {
          this.topData = res.data
          this.searchForm.totalCount = res.totalCount;
          this.searchForm.size = res.pageSize
        }
      })
    },
    getYearData () {
      this.$api.recycleBin.getAdminBackStopCount().then(res => {
        if (res.code == 200) {
          this.topData = res.data

        }
      })
    },
    handleReset () {
      
      this.searchForm={
        info:'',
      }
      this.getTableData(this.searchForm.current, this.searchForm.size)
    },
    search () {
      this.getTableData(this.searchForm.info);
    },
    btnlist () {
      //获取接口
      this.$api.city.getList({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {
      this.AddFormVisible = true;
    },
    update () {
       if (this.tableSelection.length != 1) {
        this.$message.error("请选中一行数据");
        return;
      }
      this.addForm = {
          brandId:this.tableSelection[0].brandId,
          name: this.tableSelection[0].brandName,
          backMoney: this.tableSelection[0].backMoney/100
     
         };
      this.AddFormVisible = true;
      this.imageUrl = this.tableSelection[0].bandImg;
 
      // this.funTit = 'update';
      // console.log(row)
      // this.addForm = row;
      // this.addForm = {
      //   backMoney: row.backMoney/100,
      //   name: row.brandName,
      //   brandId: row.brandId
      // }
    
      // this.imageUrl = row.bandImg;
      // console.log("编辑开始")
      // if (this.tableSelection.length != 1) {
      //   this.$message.error('请选中一行数据');
      //   return
      // }
      // this.addForm.name = this.tableSelection[0].name;
      // console.log("这里的backMoney="+this.addForm.backMoney)
      // this.addForm.backMoney = this.tableSelection[0].backMoney/100;
      // this.AddFormVisible = true;
      // console.log(this.addForm, '234234')
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.getTableData(this.searchForm.current, this.searchForm.size)
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.state == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.shopList.updateStatus({
          brandId: demo.brandId,
          state: 1
        }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.cid, this.searchForm.info);
            this.$message.success('启用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.state == 2) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.shopList.updateStatus({
          brandId: demo.brandId,
          state: 2
        }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.cid, this.searchForm.info);
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    }
  }
}
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.img-feng {
  width: 80%;
  height: 80%;

  
}
.table-box {
  text-align: center;
}
.btn-box {
  margin-bottom: 20px;
}
.img-box {
  width: 50px;
  height: 50px;
}
.check-user-box {
  margin-left: 20px;
  margin-bottom: 20px;
}
.user-box-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.fontText {
  font-size: 20px;
}

.fontTop {
  font-size: 40px;
}

.top-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.top-item {
  border: 1px gray solid;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  padding: 20px;

  .top-icon {
    width: 50%;
    height: 150px;
    border: 1px red solid;
  }

  .right-item-box {
    text-align: center;
    margin-left: 20px;
  }
}
</style>
